<template>
	<base-page class="base-form" title="通知管理" @back="onCancel()">
		<view class="main-box" style="padding: 0rpx 32rpx">
			<view class="example">
				<uni-forms ref="valiForm" :rules="rules" :modelValue="valiFormData" label-width="120px">
					<uni-forms-item label="班级" required name="classId">
						<uni-data-select v-model="valiFormData.classId" :localdata="classInfo"
							placeholder="请选择发布通知的班级"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="标题" required name="title">
						<uni-easyinput v-model="valiFormData.title" placeholder="请输入标题" />
					</uni-forms-item>
					<uni-forms-item label="活动开始时间" >
						<uni-datetime-picker type="datetime" v-model="valiFormData.startTime"  />
					</uni-forms-item>
					<uni-forms-item label="老师" >
						<uni-data-select v-model="valiFormData.teacherId" :localdata="teacherInfo"
							placeholder="请选择发布通知的老师"></uni-data-select>
					</uni-forms-item>
					<uni-forms-item label="发布"  name="isAnnounce">
						<view @click="changeStatus"
							style="width: 100%;box-sizing: border-box;border-radius: 4px;border: 1px solid #e5e5e5;display: flex;">
							<picker style="padding: 10px; width: 100%;" @change="bindPickerChange" :value="index"
								:range="columns">
								<view class="uni-input">{{ columns[index] }}</view>
							</picker>
						</view>
					</uni-forms-item>
					<uni-forms-item label="内容" required name="intro">
						<textarea v-model="valiFormData.intro" maxlength="999999"
							style="width:100% !important;min-height:400px !important;font-size: 14px !important;"
							class="textarea" type="textarea" placeholder="请输入活动内容" />
					</uni-forms-item>
				</uni-forms>
				<button type="primary" @click="submit('valiForm')" >提交</button>
				<u-toast ref="uToast" />
			</view>
		</view>
	</base-page>
</template>

<script>
	import {
		CourseInfoController
	} from "@/api";

	export default {
		data() {
			return {
				openSubmit: true,
				index: 0,
				columns: ['否', '是'],
				show2: false,
				// 校验表单数据
				valiFormData: {
					name: '',
					title: '',
					intro: '',
					isAnnounce: '',
					startTime: ""
				},
				// 校验规则
				rules: {
					classId: {
						rules: [{
							required: true,
							errorMessage: '班级不能为空'
						}]
					},
					title: {
						rules: [{
							required: true,
							errorMessage: '标题不能为空'
						}]
					}
				},
				classInfo: [],
				teacherInfo: [],
				type: 'textarea',
				border: true,
				show: false,
				startTime: '',
				form: {
					title: '',
					intro: '',
				},
				jsData: {},
				params: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: true
				},
				sendStatus: 0,
			};
		},
		mounted() {},
		methods: {
			onCancel() {
				uni.navigateBack();
			},
			bindPickerChange(e) {
				this.index = e.detail.value
			},
			changeStatus() {
				this.show2 = true;
			},
			selectTime() {
				this.show = true;
			},
			confirm(e) {
				this.input = '';
				if (this.params.year) this.input += e.year;
				if (this.params.month) this.input += '-' + e.month;
				if (this.params.day) this.input += '-' + e.day;
				if (this.params.hour) this.input += ' ' + e.hour;
				if (this.params.minute) this.input += ':' + e.minute;
				if (this.params.second) this.input += ':' + e.second;
				this.valiFormData.startTime = this.input;
			},
			showToast() {
				this.$refs.uToast.show({
					title: '操作成功',
					type: 'success',
				})

				setTimeout(function() {
					uni.navigateBack({
						delta: 1
					});
				}, 2000)

			},
			showErro() {
				this.$refs.uToast.show({
					title: '操作失败',
					type: 'error',
				})
			},
			submit(ref) {
				this.$refs[ref].validate().then(res => {
					this.openSubmit = false;
					if (res) {
						let classActivity = {
							classActivityDto:{
								"classId": this.valiFormData.classId,
								"title": this.valiFormData.title,
								"content": this.valiFormData.intro,
								"isAnnounce": this.index,
								"startTime": this.valiFormData.startTime.trimEnd(),
								"teacherId": this.valiFormData.teacherId,
							}

						}
						CourseInfoController.classActivityAdd(this,classActivity).then((res) => {
							if (res) {
								this.showToast();
							} else {
								this.showErro();
							}
						})
					} else {

					}
				})
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			// this.$refs.uForm.setRules(this.rules);
		},
		onLoad(data) {
			// this.classInfo = JSON.parse(decodeURIComponent(data.data))
			// this.valiFormData.name = this.classInfo.className;
			// this.jsData = JSON.parse(decodeURIComponent(data.data))
			CourseInfoController.listDict(this)
				.then((res) => {
					this.classInfo = res.map(x => {
						return {
							text: x.name,
							value: x.id
						}
					})

				})
				.catch((e) => {});
			CourseInfoController.teacherInfo(this)
				.then((res) => {
					this.teacherInfo = res.map(x => {
						return {
							text: x.name,
							value: x.id
						}
					})

				})
				.catch((e) => {});
		}
	}
</script>

<style lang="scss" scoped>
	.textarea {
		display: flex;
		box-sizing: border-box;
		flex-direction: row;
		align-items: center;
		border: 1px solid #dcdfe6;
		// border-radius: 4px;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>